import {
	apiGetYuanData,
	apiGetYuanDataDeatil,
	apiDelYuanData,
	apiAddYuanData,
	apiUpdataYuanData,
	apiConnDataList,
	apiConnDataDetail,
	apiConnDataDel,
	apiConnDataAdd,
	apiConnDataUpdata,
	apiGetArea,
	apiGetYwType,
	apiGetType
} from "./apiFuwuServer"

export const componentGetYuanData = (ev,param) => {/*获取元数据*/
 				apiGetYuanData(
 					{
						"search":param.search,//字段值
						"sortTime":param.sortTime,//创建时间排序字段，只能写asc(升序)或desc（降序），不写默认asc
						"sortNumber":param.sortNumber,//引用数量排序，只能写asc(升序)或desc（降序）
						"pageNum":param.pageNum,//分页页数，从1开始
						"pageSize":param.pageSize//显示条数
					}
 				).then((res)=>{
 					if(res.data.code==='S100101'){
 						console.log(res);
 						ev.dataManage = res.data.data.list;
 					}
 				})
};

export const componentGetYuanDataDeatil = (ev,param) => {/*获取元数据详情*/
 				apiGetYuanDataDeatil(param).then((res)=>{
 					if(res.data.code==='S100101'){
						ev.detailData = res.data.data;
 					}
 				})
};

export const componentDelYuanData = (ev,param) => {/*删除元数据*/
	        		ev.$confirm('是否删除?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(() => {
				        apiDelYuanData(param).then((res)=>{
				        	console.log(res);
		 					if(res.data.code==='S100101'){
		 					    ev.$message({
					            type: 'success',
					            message: '删除成功!'
					          });
					       }
		 				})
			        }).catch(() => {
			          ev.$message({
			            type: 'info',
			            message: '已取消删除'
			          });          
			        });
	

};



export const componentAddYuanData = (ev,param)=>{/*插入元数据*/
	console.log(param);
	apiAddYuanData({
		"name":param.zdName,
		"key":param.zdName,
		"dataType":param.type,
		"type":param.shiyongType,
		"minLen":param.minWidth,
		"maxLen":param.maxWidth,
		"ruleIds":param.resource,
		"otherRule":param.desc,
	}).then((res)=>{
		console.log(res);
		if(res.data.code==='S100101'){
				    ev.$message({
					            type: 'success',
					            message: '新增成功!'
					          });
		}else{
			ev.$message({
			            type: 'info',
			            message: res.data.msg
			     });          
		}
	})
}

export const componentUpdataYuanData = (ev,ids,param)=>{/*修改元数据*/
	console.log(ids);
	apiUpdataYuanData({
		"id":ids.id,
		"name":param.zdName,
		"key":param.zdName,
		"dataType":param.type,
		"type":param.shiyongType,
		"minLen":param.minWidth,
		"maxLen":param.maxWidth,
		"ruleIds":param.resource,
		"otherRule":param.desc,
	}).then((res)=>{
		console.log(res);
		if(res.data.code==='S100101'){
				    ev.$message({
					            type: 'success',
					            message: '修改成功!'
					          });
		}else{
			ev.$message({
			            type: 'info',
			            message: res.data.msg
			     });          
		}
	})
}

export const componentConnDataList = (ev,param)=>{/*数据源列表*/
	let type = ["","csv文件","mysql5.7","oracle11","xml文件","json文件"];
	apiConnDataList({
		"name":param.name,
		"pageNum":param.pageNum,
		"pageSize":param.pageSize,
	}).then((res)=>{
			console.log(res);
			if(res.data.code==='S100101'){
//				for(let i in res.data.data.list){
//					res.data.data.list[i].type = type[res.data.data.list[i].type];
//				}
				ev.tableData = res.data.data.list;
			}
			
	})
}


export const componentConnDataDetail = (ev,param)=>{/*数据源详情*/
	apiConnDataDetail(param).then((res)=>{
			console.log(res);
			if(res.data.code==='S100101'){
					ev.formData[0].value=res.data.data.name;
					ev.formData[1].value=res.data.data.type;
					ev.formData[2].value=res.data.data.area;
					ev.formData[3].value=res.data.data.libraryType;
					ev.formData[4].value=res.data.data.dataSourceVersion;
					ev.formData[5].value=res.data.data.address;
					ev.formData[6].value=res.data.data.dataBaseName;
					ev.formData[7].value=res.data.data.describe;
					ev.formData[8].value = res.data.data.isConn===0?'链接不通':'链接成功';
			}
			
	})
}

export const componentConnDataDel = (ev,param)=>{/*删除数据源*/
		
		 	ev.$confirm('是否删除?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(() => {
			        	apiConnDataDel(param).then((res)=>{
			        		if(res.data.code==='S100101'){
			        			   ev.$message({
						            type: 'success',
						            message: '删除成功!'
						          });
			        		}else{
			        			ev.$message({
						            type: 'info',
						            message:'删除失败'
						          });          
			        		}
						})
			        }).catch(() => {
			          ev.$message({
			            type: 'info',
			            message: res.data.msg
			          });          
			        });
}
        		    

export const componentConnDataAdd = (ev,param)=>{/*增加数据源*/
	apiConnDataAdd(param).then((res)=>{
			console.log(res);
			if(res.data.code==='S100101'){
					ev.$message({
						type: 'success',
						message: '添加成功!'
					});
			}else{
			        ev.$message({
						 type: 'info',
						  message:'添加失败'
					});          
			      }
			
	})
}

export const componentConnDataUpdata = (ev,param,id)=>{/*修改数据源*/
	param.id = id;
	console.log(param);
	apiConnDataUpdata(param).then((res)=>{
			console.log(res);
			if(res.data.code==='S100101'){
					ev.$message({
						type: 'success',
						message: '修改成功!'
					});
			}else{
			        ev.$message({
						 type: 'info',
						  message:'修改失败'
					});          
			}
	})
}

export const componentGetArea = (ev,param)=>{/*获取所在区域*/
	apiGetArea(param).then((res)=>{
		if(res.data.code==='S100101'){
			console.log(res);
			ev.formLabel[2].options = res.data.data.list;
		}
	})
}


export const componentGetYwType = (ev,param) => {/*获取业务类型，网段区域，信息类型*/
 				apiGetYwType(param).then((res)=>{
 					if(res.data.code==='S100101'){
 						console.log(res);
 						if(res.data.data.list[0].type==="segment"){
 							ev.formLabel[1].options = res.data.data.list[0].childList;
 						}
 					}
 				})
};

export const componentGetType = (ev,param) => {/*获取业务类型，网段区域，信息类型*/
   				apiGetYwType(param).then((res)=>{
   					if(res.data.code==='S100101'){
   						if(res.data.data.list[0].type==="db_type"){
   							ev.formLabel[4].options = res.data.data.list[0].childList;
   						}
   					}
   					console.log(ev.formLabel);
   				})
};
